import { Controller, Post, Get, Request, UseGuards,Body } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from 'src/core/auth/auth.service';
import { AuthGuard } from '@nestjs/passport';
import { LoginUserDTO } from 'src/dto/user/login-user.dto';
import { Public } from 'src/common/decorators/public.decorator';
import { UserService } from './user.service';
import { UserRegisterDto } from 'src/dto/user/user-register.dto';

@ApiTags('user')
// swagger请求头携带token发生请求
@ApiBearerAuth()
@Controller('user')
export class UserController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UserService) { }
  @Public()
  @ApiOperation({ summary: "用户登录" })
  @UseGuards(AuthGuard('local'))
  @Post("login")
  @ApiBody({
    type: LoginUserDTO,
    description: "请求体参数"
  })
  async login(@Request() req: any) {
    console.log("gggggggggS", req.user);
    return this.authService.login(req.user);
  }

  @Public()
  @ApiOperation({ summary: "注册用户(不用携带token)" })
  @Post("register")
  async register(@Body() user: UserRegisterDto): Promise<any> {
    await this.userService.register(user);
    return "注册成功"
  }

  @ApiOperation({ summary: "测试token是否通过获取用户信息" })
  // @UseGuards(AuthGuard('jwt'))
  @Get("profile")
  getProfile(@Request() req: any) {
    // console.log('profile :>> ', req);
    return req.user
  }

  @ApiOperation({ summary: "获取全部用户信息" })
  // @UseGuards(AuthGuard('jwt'))
  @Get("getUserAll")
  async getUserAll(@Request() req: any) {
   const res = await this.userService.findAll();
    // console.log('profile :>> ', req);
    return res
  }

}


